<template>
  <div>
    <div style="margin-bottom: 10px;margin-top: 10px;">
          <el-input v-model="createUserName"  placeholder="请输入产物名" suffix-icon="el-icon-search" style="width: 200px"
          @keyup.enter.native="loadPost"></el-input>
          <el-select v-model="activityId"  placeholder="请选择活动"  style="margin-left: 10px">
           <el-option
            v-for="item in activityIds"
            :key="item.value"
            :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>

          <el-select v-model="status" placeholder="请选择状态"  style="margin-left: 10px">
           <el-option
            v-for="item in statuss"
            :key="item.value"
            :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
       
     
          <el-button type="primary" style="margin-left: 10px" @click="loadPost">查询</el-button>
          <el-button type="success" @click="resetParam">重置</el-button>
          <el-button type="primary" style="margin-left: 10px" @click="add">新增</el-button>
      </div>
  <el-table :data="tableData"
  :header-cell-style="{background:'#f2f5fc',color:'#555'}" border>
      <el-table-column prop="id" label="ID" width="160">
      </el-table-column>

      <el-table-column prop="activityId" label="活动名" width="160" :formatter="titleFormate">
      </el-table-column>
 
      <el-table-column prop="achieveId" label="产物名"  width="160"  :formatter="achievementFormate">
      </el-table-column>

      <el-table-column prop="status" label="状态"  width="160" :formatter="statusFormate">
      </el-table-column>

      

      <el-table-column prop="createUserName" label="创建人姓名"  width="160">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"  width="160">
      </el-table-column>


      
      <el-table-column prop="updateUserName" label="修改人姓名"  width="160">
      </el-table-column>
      <el-table-column prop="updateTime" label="修改时间"  width="160">
      </el-table-column>


      
      <el-table-column prop="operate" label="操作">
          <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
              
          <el-popconfirm
                      style="margin-left: 5px;"
                      title="这是一段内容确定删除吗？"
                      @confirm="del(scope.row.id)"
              >
                  <el-button slot="reference" size="small" type="danger">删除</el-button>
              </el-popconfirm>
          </template>
      </el-table-column>

  </el-table>
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pagenum"
              :page-sizes="[2, 5, 10, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
      </el-pagination>

      <el-dialog
              title="提示"
              :visible.sync="centerDialogVisible"
              width="80%"
              center>
<!--           //增加表单-->
          <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
          
            
            

             
                      <el-form-item label="选择参加活动的产物">
                  <el-select v-model="form.type" placeholder="请选择活动类型">
                  <el-option label="线上" value="1"></el-option>
                  <el-option label="线下" value="2"></el-option>
                   </el-select>
                </el-form-item>

                <el-form-item label="活动名" prop="title">
                      <el-input  v-model="form.title"></el-input>
              </el-form-item>



                <el-form-item label="活动内容" prop="content">
                      <el-input v-model="form.content"></el-input>
                </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="centerDialogVisible = false">取 消</el-button>
           <el-button type="primary" @click="save">确 定</el-button>
            </span>
      </el-dialog>
  </div>
</template>

<script>
    export default {
        name: "UnitList",
        data() {
         
            return {
                // 查询下拉框
                statuss: [{
          value: -1,
          label: '全部状态'
        }, {
          value: 1,
          label: '审核中'
        }, {
          value: 2,
          label: '已通过'
        },
        {
          value: 3,
          label: '未通过'
        }],
        status: -1,
        activityId:-1,
        activityIds:[{
            value: -1,
            label: '全部活动'
            }, {
              value: 1,
              label: '重庆财经学院第三届讯飞杯'
                },
                {
              value: 2,
              label: '重庆马拉松赛事'
                },
              ],
              createUserName:'',
              ActivityData:[],
              tableData: [],
              AchievementData:[],
              pagenum:1,
              pageSize:2,
              total:0,
                centerDialogVisible:false,
                form:{
                    title:'',
                    name:'',
                    content:'',
                 

                },
                rules: {
                   
                    name: [
                        { required: true, message: '请输入名字', trigger: 'blur' },
                    ],
                   

                }

            }

        },
        methods:{
            
            achievementFormate(row, index){
                for(let i in this.AchievementData){
                    if(this.AchievementData[i].id==row.achieveId){
                        return this.AchievementData[i].name
                    }
                }
            },
            titleFormate(row, index){
                for(let i in this.ActivityData){
                    if(this.ActivityData[i].id==row.activityId){
                        return this.ActivityData[i].title
                    }
                }
            },
             // 显示状态
           statusFormate(row, index){
                if(row.status == 1){
                    return "筹划中";
                }else if(row.status == 2){
                    return "已发布";
                }else if(row.status == 3){
                    return "已下架";
                }},
            resetForm() {
                this.$refs.form.resetFields();
            },
            mod(row){
           
            //    赋值到表单

                this.centerDialogVisible=true
                this.$nextTick(()=>{
                    this.form.id = row.id
                    this.form.title = row.title
                    this.form.content = row.content
                    this.form.name = row.name
                   
                 
                    //    这个在第一次的时候不能使用，所以需要nextTick异步
                })
            },
            // 删除
            del(id){
                this.$axios.get(this.$httpUrl+'/admin/activity-apply/remove?id='+id).then(res=>res.data).then(res=>{

                    if (res.code=='00000'){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面
                        this.loadPost()
                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })

            },
            add(){

                this.centerDialogVisible=true
                this.$nextTick(()=>{
                    this.resetForm()
                //    这个在第一次的时候不能使用，所以需要nextTick异步
                })
            },
            // 修改
            doMod(){
                this.$axios.post(this.$httpUrl+'/admin/activity-apply/update',
                    this.form

                ).then(res=>res.data).then(res=>{

                    if (res.code=='00000'){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面

                        this.centerDialogVisible=false
                        this.loadPost()
                        // this.resetForm()

                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })
            },
            // 增加保存
            doSave(){
                this.$axios.post(this.$httpUrl+'/admin/activity-apply/add',
                    this.form

                ).then(res=>res.data).then(res=>{

                    if (res.code==200){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面

                        this.centerDialogVisible=false
                        this.loadPost()
                        // this.resetForm()

                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })
            },
            save(){
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        if (this.form.id){
                            this.doMod()
                        }else{
                            this.doSave()
                        }

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            handleSizeChange(val) {
                
                this.pagenum=1
                this.pageSize=val
                this.loadPost()
            },
            handleCurrentChange(val) {
                
                this.pagenum=val
                this.loadPost()
            },
          
            resetParam(){
                this.createUserName='',
                this.status=-1,

                this.activityId=-1
           
                
            },
            loadPost(){
             
             this.$axios.get(this.$httpUrl+'/jk/activity/apply/list?status='+this.status+'&createUserName='+this.createUserName+'&activityId='+this.activityId+'&pagenum='+this.pagenum +'&pageSize='+this.pageSize
             ).then(res=>res.data).then(res=>{
               console.log(res)
                 if (res.resCode=='000000'){
                     this.tableData=res.data.records
                     this.total=res.data.total

                 }else{
                     alert('获取数据失败')
                 }

             })
         },
        //  获取活动
        loadActivity(){
             
             this.$axios.get(this.$httpUrl+'/jk/admin/activity/listAll').then(res=>res.data).then(res=>{
                
                 if (res.resCode=='000000'){
                     this.ActivityData=res.data
                     console.log(this.ActivityData)

                 }else{
                     alert('获取数据失败')
                 }

             })
         },
// 获取成果
loadAchievement(){
             
             this.$axios.get(this.$httpUrl+'/jk/admin/achievement/listAll').then(res=>res.data).then(res=>{
                
                 if (res.resCode=='000000'){
                     this.AchievementData=res.data

                 }else{
                     alert('获取数据失败')
                 }

             })
         },
        },
        beforeMount() {
            this.loadAchievement()
            this.loadActivity()
            this.loadPost();


        }
    }
</script>

<style>

</style>